let api = [];
const apiDocListSize = 1
api.push({
    name: 'default',
    order: '1',
    list: []
})
api[0].list.push({
    alias: 'UserTagController',
    order: '1',
    link: '用户标签管理',
    desc: '用户标签管理',
    list: []
})
api[0].list[0].list.push({
    order: '1',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/tag/listTag',
    methodId: 'a41cb3380f38cbb3c0f3be4bd060d7e4',
    desc: '获取用户标签',
});
api[0].list[0].list.push({
    order: '2',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/tag/setTag',
    methodId: '802de14d8d427c6d6623e217cd4f8ffd',
    desc: '设置标签',
});
api[0].list[0].list.push({
    order: '3',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/tag/cancelTag',
    methodId: '2e4b595075d7da8df6624b8ed304b8dd',
    desc: '取消标签',
});
api[0].list[0].list.push({
    order: '4',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/tag/containTag',
    methodId: 'cbfed5b163891bb4be414d43e7e1716e',
    desc: '是否包含某个标签',
});
api[0].list.push({
    alias: 'UserController',
    order: '2',
    link: '用户管理',
    desc: '用户管理',
    list: []
})
api[0].list[1].list.push({
    order: '1',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/list',
    methodId: '896b96647509a4197a9c83f7c7522b62',
    desc: '用户列表',
});
api[0].list[1].list.push({
    order: '2',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/add',
    methodId: '5d3deb388869eeaf62c703f79bd02f27',
    desc: '新增用户',
});
api[0].list[1].list.push({
    order: '3',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/getUserInfo',
    methodId: '60eb9febb60170b32595c120d222b501',
    desc: '获取用户信息',
});
api[0].list[1].list.push({
    order: '4',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/updateUserInfo',
    methodId: '850da0ef04e33f5427f060475e8d1b0f',
    desc: '更新用户信息',
});
api[0].list[1].list.push({
    order: '5',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/updateAvatar',
    methodId: '4a0336d834611c8af3389f32c8eec65c',
    desc: '更新用户头像',
});
api[0].list[1].list.push({
    order: '6',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/updatePassword',
    methodId: '6218fc43fb9aed01f56bb86d74acd952',
    desc: '修改用户密码',
});
api[0].list[1].list.push({
    order: '7',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/delete/{userId}',
    methodId: '2653959aaf306ece2d38339d28486ec8',
    desc: '删除用户',
});
api[0].list[1].list.push({
    order: '8',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/updateStatus',
    methodId: '9e1aeea9f8d1510565ce5bf794ed3414',
    desc: '状态修改',
});
api[0].list.push({
    alias: 'OAuth2Controller',
    order: '3',
    link: 'oauth2登录控制',
    desc: 'OAuth2登录控制',
    list: []
})
api[0].list[2].list.push({
    order: '1',
    deprecated: 'false',
    url: 'http://localhost:9000/api/auth/oauth2/login/{provider}',
    methodId: 'f5bb2b69b6389dd335de9073ebd869dc',
    desc: '第三方登录重定向',
});
api[0].list[2].list.push({
    order: '2',
    deprecated: 'false',
    url: 'http://localhost:9000/api/auth/oauth2/callback/{provider}',
    methodId: '58ee7a15d7b159cb89d1ee36dbe0e24e',
    desc: '第三方登录回调处理',
});
api[0].list.push({
    alias: 'AuthController',
    order: '4',
    link: '认证权限管理',
    desc: '认证权限管理',
    list: []
})
api[0].list[3].list.push({
    order: '1',
    deprecated: 'false',
    url: 'http://localhost:9000/api/auth/login',
    methodId: '4446dd7963651f4e9c9d6a92b26fdf2a',
    desc: '用户登录',
});
api[0].list[3].list.push({
    order: '2',
    deprecated: 'false',
    url: 'http://localhost:9000/api/auth/register',
    methodId: 'b8970ced9e84e213ff753565eeb35219',
    desc: '用户注册',
});
api[0].list[3].list.push({
    order: '3',
    deprecated: 'false',
    url: 'http://localhost:9000/api/auth/logout',
    methodId: '546b42094d5b60d583c5be79226e24ab',
    desc: '用户登出',
});
api[0].list[3].list.push({
    order: '4',
    deprecated: 'false',
    url: 'http://localhost:9000/api/auth/refresh',
    methodId: '345a65ac758cfd7d4d7a0c57927339a9',
    desc: '刷新token',
});
api[0].list.push({
    alias: 'UserCheckinsController',
    order: '5',
    link: '用户打卡管理',
    desc: '用户打卡管理',
    list: []
})
api[0].list[4].list.push({
    order: '1',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/checkins/checkin',
    methodId: '29f8dcd63091e6e6db14be46a45ea0c9',
    desc: '用户打卡',
});
api[0].list[4].list.push({
    order: '2',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/checkins/list/{userId}',
    methodId: 'f96714d3129d123703e3b77ae3e11614',
    desc: '获取用户打卡记录（分页）',
});
api[0].list[4].list.push({
    order: '3',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/checkins/{checkinId}',
    methodId: 'dc6d2ea0a4389537ec2a3bf4b1f0ee2c',
    desc: '获取打卡详情',
});
api[0].list[4].list.push({
    order: '4',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/checkins/{checkinId}',
    methodId: '34bdfca2b3a505c612f9d93faa0e0a80',
    desc: '更新打卡信息',
});
api[0].list[4].list.push({
    order: '5',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/checkins/{checkinId}',
    methodId: '163c29c9e4ec4c31eb6d0df0492b172b',
    desc: '删除打卡记录',
});
api[0].list.push({
    alias: 'UserPreferencesController',
    order: '6',
    link: '用户偏好设置控制器',
    desc: '用户偏好设置控制器',
    list: []
})
api[0].list[5].list.push({
    order: '1',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/preferences',
    methodId: 'c23b50d7a2109df702e64ca417f59e8a',
    desc: '获取用户偏好设置',
});
api[0].list[5].list.push({
    order: '2',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/preferences',
    methodId: 'd221080f4cb0a7928a8d533abbbe2340',
    desc: '更新用户偏好设置',
});
api[0].list[5].list.push({
    order: '3',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/preferences/reset',
    methodId: 'e50741998985172677e264cf16139aea',
    desc: '重置用户偏好设置为默认值',
});
api[0].list.push({
    alias: 'UserFollowsController',
    order: '7',
    link: '用户关注、粉丝管理',
    desc: '用户关注、粉丝管理',
    list: []
})
api[0].list[6].list.push({
    order: '1',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/follows/follow/{followedId}',
    methodId: '28d3ebf04b9ed29c80ab7e61f43d76df',
    desc: '关注用户',
});
api[0].list[6].list.push({
    order: '2',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/follows/unfollow/{followedId}',
    methodId: '74e4f641b2e4bb9ba5486c6238765d67',
    desc: '取消关注',
});
api[0].list[6].list.push({
    order: '3',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/follows/check/{followedId}',
    methodId: '9336c3a5341b4529ed60520bb2713ba1',
    desc: '检查是否已关注',
});
api[0].list[6].list.push({
    order: '4',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/follows/following',
    methodId: '9ef0ddb33e692f4d4fc5e66dfd2ef00f',
    desc: '获取关注列表',
});
api[0].list[6].list.push({
    order: '5',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/follows/followers',
    methodId: 'cdde044f903729477254ef8657cc4d68',
    desc: '获取粉丝列表',
});
api[0].list[6].list.push({
    order: '6',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/follows/following/count',
    methodId: '915fa4b2d38b3b5560f81cfd437c2ee0',
    desc: '获取关注数量',
});
api[0].list[6].list.push({
    order: '7',
    deprecated: 'false',
    url: 'http://localhost:9000/api/user/follows/followers/count',
    methodId: '0dfd8d737fa9850bc0dcc4a6b30a07d1',
    desc: '获取粉丝数量',
});
document.onkeydown = keyDownSearch;
function keyDownSearch(e) {
    const theEvent = e;
    const code = theEvent.keyCode || theEvent.which || theEvent.charCode;
    if (code === 13) {
        const search = document.getElementById('search');
        const searchValue = search.value.toLocaleLowerCase();

        let searchGroup = [];
        for (let i = 0; i < api.length; i++) {

            let apiGroup = api[i];

            let searchArr = [];
            for (let i = 0; i < apiGroup.list.length; i++) {
                let apiData = apiGroup.list[i];
                const desc = apiData.desc;
                if (desc.toLocaleLowerCase().indexOf(searchValue) > -1) {
                    searchArr.push({
                        order: apiData.order,
                        desc: apiData.desc,
                        link: apiData.link,
                        alias: apiData.alias,
                        list: apiData.list
                    });
                } else {
                    let methodList = apiData.list || [];
                    let methodListTemp = [];
                    for (let j = 0; j < methodList.length; j++) {
                        const methodData = methodList[j];
                        const methodDesc = methodData.desc;
                        if (methodDesc.toLocaleLowerCase().indexOf(searchValue) > -1) {
                            methodListTemp.push(methodData);
                            break;
                        }
                    }
                    if (methodListTemp.length > 0) {
                        const data = {
                            order: apiData.order,
                            desc: apiData.desc,
                            link: apiData.link,
                            alias: apiData.alias,
                            list: methodListTemp
                        };
                        searchArr.push(data);
                    }
                }
            }
            if (apiGroup.name.toLocaleLowerCase().indexOf(searchValue) > -1) {
                searchGroup.push({
                    name: apiGroup.name,
                    order: apiGroup.order,
                    list: searchArr
                });
                continue;
            }
            if (searchArr.length === 0) {
                continue;
            }
            searchGroup.push({
                name: apiGroup.name,
                order: apiGroup.order,
                list: searchArr
            });
        }
        let html;
        if (searchValue === '') {
            const liClass = "";
            const display = "display: none";
            html = buildAccordion(api,liClass,display);
            document.getElementById('accordion').innerHTML = html;
        } else {
            const liClass = "open";
            const display = "display: block";
            html = buildAccordion(searchGroup,liClass,display);
            document.getElementById('accordion').innerHTML = html;
        }
        const Accordion = function (el, multiple) {
            this.el = el || {};
            this.multiple = multiple || false;
            const links = this.el.find('.dd');
            links.on('click', {el: this.el, multiple: this.multiple}, this.dropdown);
        };
        Accordion.prototype.dropdown = function (e) {
            const $el = e.data.el;
            let $this = $(this), $next = $this.next();
            $next.slideToggle();
            $this.parent().toggleClass('open');
            if (!e.data.multiple) {
                $el.find('.submenu').not($next).slideUp("20").parent().removeClass('open');
            }
        };
        new Accordion($('#accordion'), false);
    }
}

function buildAccordion(apiGroups, liClass, display) {
    let html = "";
    if (apiGroups.length > 0) {
        if (apiDocListSize === 1) {
            let apiData = apiGroups[0].list;
            let order = apiGroups[0].order;
            for (let j = 0; j < apiData.length; j++) {
                html += '<li class="'+liClass+'">';
                html += '<a class="dd" href="#' + apiData[j].alias + '">' + apiData[j].order + '.&nbsp;' + apiData[j].desc + '</a>';
                html += '<ul class="sectlevel2" style="'+display+'">';
                let doc = apiData[j].list;
                for (let m = 0; m < doc.length; m++) {
                    let spanString;
                    if (doc[m].deprecated === 'true') {
                        spanString='<span class="line-through">';
                    } else {
                        spanString='<span>';
                    }
                    html += '<li><a href="#' + doc[m].methodId + '">' + apiData[j].order + '.' + doc[m].order + '.&nbsp;' + spanString + doc[m].desc + '<span></a> </li>';
                }
                html += '</ul>';
                html += '</li>';
            }
        } else {
            for (let i = 0; i < apiGroups.length; i++) {
                let apiGroup = apiGroups[i];
                html += '<li class="'+liClass+'">';
                html += '<a class="dd" href="#_'+apiGroup.order+'_' + apiGroup.name + '">' + apiGroup.order + '.&nbsp;' + apiGroup.name + '</a>';
                html += '<ul class="sectlevel1">';

                let apiData = apiGroup.list;
                for (let j = 0; j < apiData.length; j++) {
                    html += '<li class="'+liClass+'">';
                    html += '<a class="dd" href="#' + apiData[j].alias + '">' +apiGroup.order+'.'+ apiData[j].order + '.&nbsp;' + apiData[j].desc + '</a>';
                    html += '<ul class="sectlevel2" style="'+display+'">';
                    let doc = apiData[j].list;
                    for (let m = 0; m < doc.length; m++) {
                       let spanString;
                       if (doc[m].deprecated === 'true') {
                           spanString='<span class="line-through">';
                       } else {
                           spanString='<span>';
                       }
                       html += '<li><a href="#' + doc[m].methodId + '">'+apiGroup.order+'.' + apiData[j].order + '.' + doc[m].order + '.&nbsp;' + spanString + doc[m].desc + '<span></a> </li>';
                   }
                    html += '</ul>';
                    html += '</li>';
                }

                html += '</ul>';
                html += '</li>';
            }
        }
    }
    return html;
}